package com.ptu.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ptu.dto.CommentDTO;
import com.ptu.dto.CommentPageQueryDTO;
import com.ptu.dto.DishDTO;
import com.ptu.entity.Comment;
import com.ptu.entity.Dish;
import com.ptu.entity.DishFlavor;
import com.ptu.mapper.CommentMapper;
import com.ptu.result.PageResult;
import com.ptu.result.Result;
import com.ptu.service.CommentService;
import com.ptu.vo.CommentVO;
import com.ptu.vo.DishVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;
import java.util.stream.Collectors;

@Service
@Slf4j
public class CommentServiceImpl implements CommentService {

    @Autowired
    private CommentMapper commentMapper;

    @Override
    public void save(Comment comment) {
        commentMapper.insert(comment);
    }


    @Override
    public CommentVO getByOrderId(Long orderId) {
        Comment comment = commentMapper.getByOrderId(orderId);
        CommentVO commentVO = new CommentVO();
        if (comment == null) {
            return null;
        }
        BeanUtils.copyProperties(comment, commentVO);
        return commentVO;
    }

    @Override
    public List<CommentVO> getByStoreId(Long storeId) {
        // 调用 commentMapper 的 getByStoreId 方法获取 Comment 列表
        List<Comment> commentList = commentMapper.getByStoreId(storeId);
        // 将 Comment 列表转换为 CommentVO 列表
        return commentList.stream()
                .map(comment -> {
                    CommentVO commentVO = new CommentVO();
                    BeanUtils.copyProperties(comment, commentVO);
                    return commentVO;
                })
                .collect(Collectors.toList());
    }

    public PageResult pageQuery(CommentPageQueryDTO commentPageQueryDTO) {
        PageHelper.startPage(commentPageQueryDTO.getPage(), commentPageQueryDTO.getPageSize());
        PageInfo<CommentVO> pageInfo = new PageInfo<>(commentMapper.pageQuery(commentPageQueryDTO));
        return new PageResult(pageInfo.getTotal(), pageInfo.getList());
    }

    /**
     *
     * @param commentDTO
     */
    public void update(CommentDTO commentDTO) {
        Comment comment = new Comment();
        BeanUtils.copyProperties(commentDTO, comment);
        comment.setReplyTime(LocalDateTime.now());
        commentMapper.update(comment);
    }


}